﻿/*	@file	r_math_matrix33.cpp
	@brief	行列
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_math_matrix33.h"
#include "r_math_def.h"
#include "r_math_vector2.h"
#include "r_math_vector3.h"
#include <math.h>
#include <string.h>

namespace r{
	namespace math{
		Matrix33 :: Matrix33() {}
		Matrix33 :: Matrix33 (
			r_f32 s11, r_f32 s12, r_f32 s13,
			r_f32 s21, r_f32 s22, r_f32 s23,
			r_f32 s31, r_f32 s32, r_f32 s33 )
		{
			_11 = s11;
			_12 = s12;
			_13 = s13;
			_21 = s21;
			_22 = s22;
			_23 = s23;
			_31 = s31;
			_32 = s32;
			_33 = s33;
		}
		Matrix33 :: Matrix33 ( const r_f32 *sv )
		{
			_11 = sv[0];
			_12 = sv[1];
			_13 = sv[2];
			_21 = sv[3];
			_22 = sv[4];
			_23 = sv[5];
			_31 = sv[6];
			_32 = sv[7];
			_33 = sv[8];
		}
		Matrix33 :: Matrix33 ( r_f32 val )
		{
			_11 = _12 = _13 = _21 = _22 = _23 = _31 = _32 = _33 = val;
		}

		void	Matrix33 :: identity ( void )
		{
			_11 = _22 = _33 = 1;
			_12 = _13 = _21 = _23 = _31 = _32 = 0;
		}

		Matrix33 Matrix33 :: operator * ( const Matrix33 &src ) const
		{
			return Matrix33 (
					   _11 * src._11 + _12 * src._21 + _13 * src._31,
					   _11 * src._12 + _12 * src._22 + _13 * src._32,
					   _11 * src._13 + _12 * src._23 + _13 * src._33,

					   _21 * src._11 + _22 * src._21 + _23 * src._31,
					   _21 * src._12 + _22 * src._22 + _23 * src._32,
					   _21 * src._13 + _22 * src._23 + _23 * src._33,

					   _31 * src._11 + _32 * src._21 + _33 * src._31,
					   _31 * src._12 + _32 * src._22 + _33 * src._32,
					   _31 * src._13 + _32 * src._23 + _33 * src._33
				   );
		}
		const Matrix33&	Matrix33 :: operator *= ( const Matrix33 &src )
		{
			*this = ( *this ) * src;
			return *this;
		}
		Matrix33 Matrix33 :: operator * ( r_f32 src ) const
		{
			return Matrix33 (
					   _11 * src,	_12 * src, _13 * src,
					   _21 * src,	_22 * src, _23 * src,
					   _31 * src,	_32 * src, _33 * src
				   );
		}
		const Matrix33&	Matrix33 :: operator *= ( r_f32 src )
		{
			*this = ( *this ) * src;
			return *this;
		}
		Matrix33 Matrix33 :: operator + ( const Matrix33 &src ) const
		{
			Matrix33 tmat (
				_11 + src._11,	_12 + src._12, _13 + src._13,
				_21 + src._21,	_22 + src._22, _23 + src._23,
				_31 + src._31,	_32 + src._32, _33 + src._33
			);
			return tmat;
		}
		const Matrix33&	Matrix33 :: operator += ( const Matrix33 &src )
		{
			*this = ( *this ) + src;
			return *this;
		}

		void	Matrix33 :: transpose ( void )
		{
			Matrix33 tmat ( *this );
		//	_11 = tmat._11;
			_21 = tmat._12;
			_31 = tmat._13;
			_12 = tmat._21;
		//	_22 = tmat._22;
			_32 = tmat._23;
			_13 = tmat._31;
			_23 = tmat._32;
		//	_33 = tmat._33;
		}

		const Matrix33&	Matrix33 :: scale ( const r::math::Vector3D &src )
		{
			_12 = _13 = _21 = _23 = _31 = _32 = 1;
			_11 = src.x;
			_22 = src.y;
			_33 = src.z;
			return *this;
		}

		const Matrix33&	Matrix33 :: scale ( r_f32 src )
		{
			_12 = _13 = _21 = _23 = _31 = _32 = 0;
			_11 = _22 = _33 = src;
			return *this;
		}

		const Matrix33&	Matrix33 :: rotateX ( r_f32 src )
		{
			_11 = _12 = _13 =
							_21 =
								_31 = 0;
			_22 = _33 = cos ( src );
			_23 = sin ( src );
			_32 = -sin ( src );
			_11 = 1;
			return *this;
		}

		const Matrix33&	Matrix33 :: rotateY ( r_f32 src )
		{
			_12 =
				_21 = _23 =
						  _32 = 0;
			_11 = _33 = cos ( src );
			_13 = -sin ( src );
			_31 = sin ( src );
			_22 = 1;
			return *this;
		}

		const Matrix33&	Matrix33 :: rotateZ ( r_f32 src )
		{
			_13 =
				_23 =
					_31 = _32 = 0;
			_11 = _22 = cos ( src );
			_12 = sin ( src );
			_21 = -sin ( src );
			_33 = 1;
			return *this;
		}

		const Matrix33&	Matrix33 :: rotateAxis ( const r::math::Vector3D &vec, r_f32 rot )
		{
			r_f32 s = sin ( rot );
			r_f32 c = cos ( rot );
			r_f32 nc = 1.0f - c;
			r_f32 sx = vec.x * s;
			r_f32 sy = vec.y * s;
			r_f32 sz = vec.z * s;
			r_f32 nx = vec.x * nc;
			r_f32 ny = vec.y * nc;
			r_f32 nz = vec.z * nc;
			_11 = vec.x * nx + c;
			_12 = vec.x * ny - sz;
			_13 = vec.x * nz + sy;
			_21 = vec.y * nx + sz;
			_22 = vec.y * ny + c;
			_23 = vec.y * nz - sx;
			_31 = vec.z * nx - sy;
			_32 = vec.z * ny + sx;
			_33 = vec.z * nz + c;
			return *this;
		}

		r::math::Vector3D	Matrix33 :: transform3D ( const r::math::Vector2D &vec ) const
		{
			return r::math::Vector3D (
					   vec.x * _11 + vec.y * _21,
					   vec.x * _12 + vec.y * _22,
					   vec.x * _13 + vec.y * _23 );
		}
		r::math::Vector3D	Matrix33 :: transform3D ( const r::math::Vector3D &vec ) const
		{
			return r::math::Vector3D (
					   vec.x * _11 + vec.y * _21 + vec.z * _31,
					   vec.x * _12 + vec.y * _22 + vec.z * _32,
					   vec.x * _13 + vec.y * _23 + vec.z * _33 );
		}
		const Matrix33&	Matrix33 :: normalize ( void )
		{
			r::math::Vector3D v1 = r::math::Vector3D ( &_11 ).normalize();
			r::math::Vector3D v2 = r::math::Vector3D ( &_21 ).normalize();
			r::math::Vector3D v3 = r::math::Vector3D ( &_31 ).normalize();
			_11 = v1.x;
			_12 = v1.y;
			_13 = v1.z;
			_21 = v2.x;
			_22 = v2.y;
			_23 = v2.z;
			_31 = v3.x;
			_32 = v3.y;
			_33 = v3.z;
			return *this;
		}

		r::math::Vector3D Matrix33 :: getSize ( void ) const
		{
			return r::math::Vector3D ( r::math::Vector3D ( &_11 ).length(), r::math::Vector3D ( &_21 ).length(), r::math::Vector3D ( &_31 ).length() );
		}

		/* 抜粋 http://www7.atwiki.jp/lucifer/pages/13.html
		６通りの結果があるので求め方も６通りあります。

		ＸＹＺの行列を任意の順序で掛けた行列Ｒの中身が下のようになっている場合、
		最初の回転軸の角度をα、２番目をβ、３番目をγとすると計算式は以下の様になります。
					xx	xy	xz
		R	=		yx	yy	yz
					zx	zy	zz
		α	β	γ			γの角度	βの角度(-90～90)	αの角度
		Ｘ	Ｙ	Ｚ			atan2(xy,xx)	-asin(xz)	asin(yz/cos(β)) if(zz<0)α=180-α
		Ｘ	Ｚ	Ｙ			atan2(xz,xx)	-asin(xy)	asin(zy/cos(β)) if(yy<0)α=180-α
		Ｙ	Ｘ	Ｚ			atan2(yx,yy)	-asin(yz)	asin(xz/cos(β)) if(zz<0)α=180-α
		Ｙ	Ｚ	Ｘ			atan2(yz,yy)	-asin(yx)	asin(zx/cos(β)) if(xx<0)α=180-α
		Ｚ	Ｘ	Ｙ			atan2(zx,zz)	-asin(zy)	asin(xy/cos(β)) if(yy<0)α=180-α
		Ｚ	Ｙ	Ｘ			atan2(zy,zz)	-asin(zx)	asin(yx/cos(β)) if(xx<0)α=180-α


		cos(β)がゼロになるときは別のやり方で計算する必要があります。
		（γ＝０、β＝９０or－９０としてαを計算するだけなので省略）

		cos(β)がゼロになるときα＝０、β＝９０or－９０としてγを計算する方法
		（γをゼロにするとベータの符号によって式を変える必要がある）
		α	β	γ			γの角度	βの角度(-90or90)	αの角度
		Ｘ	Ｙ	Ｚ			atan2(-yx,yy)	-asin(xz)	0
		Ｘ	Ｚ	Ｙ			atan2(zx,zz)	-asin(xy)	0
		Ｙ	Ｘ	Ｚ			atan2(xy,xx)	-asin(yz)	0
		Ｙ	Ｚ	Ｘ			atan2(-zy,zz)	-asin(yx)	0
		Ｚ	Ｘ	Ｙ			atan2(-xz,xx)	-asin(zy)	0
		Ｚ	Ｙ	Ｘ			atan2(yz,yy)	-asin(zx)	0
		*/
		r::math::Vector3D	Matrix33 :: EulerXYZ ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._13 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( -mat._21, mat._22 );
				a = 0;
			} else {
				c = atan2f ( mat._12, mat._11 );
				a = asinf ( mat._23 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		r::math::Vector3D	Matrix33 :: EulerXZY ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._12 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( mat._31, mat._33 );
				a = 0;
			} else {
				c = atan2f ( mat._13, mat._11 );
				a = asinf ( mat._32 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		r::math::Vector3D	Matrix33 :: EulerYXZ ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._23 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( mat._12, mat._11 );
				a = 0;
			} else {
				c = atan2f ( mat._21, mat._22 );
				a = asinf ( mat._13 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		r::math::Vector3D	Matrix33 :: EulerYZX ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._21 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( -mat._32, mat._33 );
				a = 0;
			} else {
				c = atan2f ( mat._23, mat._22 );
				a = asinf ( mat._31 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		r::math::Vector3D	Matrix33 :: EulerZXY ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._32 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( -mat._13, mat._11 );
				a = 0;
			} else {
				c = atan2f ( mat._31, mat._33 );
				a = asinf ( mat._12 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		r::math::Vector3D	Matrix33 :: EulerZYX ( void ) const
		{
			Matrix33 mat = *this;
			mat.normalize();
			r_f32 a, b, c;
			b = -asin ( mat._31 );
			r_f32 cosb = cosf ( b );
			if ( r::math::fisZero ( cosb ) ) {
				c = atan2f ( mat._23, mat._22 );
				a = 0;
			} else {
				c = atan2f ( mat._32, mat._33 );
				a = asinf ( mat._21 / cosb );
			}
			return r::math::Vector3D ( a, b, c );
		}

		const Matrix33&	Matrix33 :: rotateXYZ ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotX * rotY * rotZ;
			return *this;
		}
		const Matrix33&	Matrix33 :: rotateXZY ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotX * rotZ * rotY;
			return *this;
		}
		const Matrix33&	Matrix33 :: rotateYXZ ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotY * rotX * rotZ;
			return *this;
		}
		const Matrix33&	Matrix33 :: rotateYZX ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotY * rotZ * rotX;
			return *this;
		}
		const Matrix33&	Matrix33 :: rotateZXY ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotZ * rotX * rotY;
			return *this;
		}
		const Matrix33&	Matrix33 :: rotateZYX ( const r::math::Vector3D &rot )
		{
			Matrix33 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotZ * rotY * rotX;
			return *this;
		}
	}
}
